/**
 * 根据输入的日期或时间戳，返回一组格式化参数
 * @param {Date|Number|String} input 日期对象、时间戳或合法日期字符串
 * @returns {Object} 包含以下字段：
 *   - isThisYear: 是否今年
 *   - isThisMonth: 是否本月
 *   - isToday: 是否当日
 *   - time: 当前时间 HH:mm:ss
 *   - monthStart: 当月第一天 00:00:00
 *   - monthEnd: 当月最后一天 23:59:59
 *   - nextMonthToday: 下个月“今天”的 YYYY-MM-DD HH:mm:ss
 *   - lastMonthToday: 上个月“今天”的 YYYY-MM-DD HH:mm:ss
 */
function getDateParams(input) {
  const date = new Date(input);
  if (isNaN(date)) throw new Error('无效日期输入');

  const now = new Date();
  const fmt = (d) => {
    const pad = (n) => n.toString().padStart(2, '0');
    return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
  };

  const startOfMonth = new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0);
  const endOfMonth   = new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59);
  const nextMonthToday = new Date(date);
  nextMonthToday.setMonth(nextMonthToday.getMonth() + 1);
  const lastMonthToday = new Date(date);
  lastMonthToday.setMonth(lastMonthToday.getMonth() - 1);

  return {
    isThisYear:  date.getFullYear() === now.getFullYear(),
    isThisMonth: date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth(),
    isToday:     date.toDateString() === now.toDateString(),
    time:        fmt(date).slice(11),
    monthStart:  fmt(startOfMonth),
    monthEnd:    fmt(endOfMonth),
    nextMonthToday: fmt(nextMonthToday),
    lastMonthToday: fmt(lastMonthToday),
    // 新增：年、月、日的拆分
    year:  date.getFullYear(),
    month: date.getMonth() + 1,
    day:   date.getDate()
  };
}

// 测试例子
console.log(getDateParams('2023-01-15 14:30:00'));
console.log(getDateParams(Date.now()));
console.log(getDateParams(new Date(2022, 0, 31))); // 2022-01-31 -> 上个月 2021-12-31
